********************************************************************************
********************************************************************************
* ACLED analysis
********************************************************************************
********************************************************************************

**** Tunisia ****
cd "/Users/christopherbarrie/Desktop/PRQ_replication_materials/acled/"

import delimited 2011-01-01-2019-01-01-Northern_Africa-Tunisia.csv, clear
gen stdate = date(event_date, "DMY")
gen event = 1
gen date = stdate
format date %tdn/d/Y
collapse(sum) event, by(date) 
save event_by_day.dta, replace

use expand, clear
merge 1:1 date using event_by_day.dta
sort date
replace event = 0 if event==.
drop _merge

gen daywk = dow(date)
recode daywk(0=0 "S")(1=1 "M")(2=2 "T")(3=3 "W")(4=4 "T")(5=5 "F")(6=6 "S"), into(dow)

preserve
collapse(sum) event, by(dow)
lab var event "#Events"

twoway bar event dow, ///
	tlabel(0 "S" 1 "M" 2 "T" 3 "W" 4 "T" 5 "F" 6 "S") ///
	aspectratio(1) ///
	xtitle("") ///
	ytitle(Protest events) legend(position(6) cols(1)) ///
	barw(0.9) ylabel(0(100)500,nogrid) name(gg1, replace)
restore 

tsset date, daily
glm event ib1.dow, family(nbinom) vce(hac nwest 1)
coefplot, xline(0) drop(_cons) ///
	xlabel(-4 -2 0 2 4) ///
	aspectratio(1) ///
	name(gg2, replace)
	
gr combine gg1 gg2, name(g1, replace) title("Tunisia")

**** Egypt ****

clear
import delimited 2011-01-01-2019-01-01-Northern_Africa-Egypt.csv, clear
gen stdate = date(event_date, "DMY")
gen event = 1
gen date = stdate
format date %tdn/d/Y
collapse(sum) event, by(date) 
save event_by_day.dta, replace

use expand, clear
merge 1:1 date using event_by_day.dta
sort date
replace event = 0 if event==.
drop _merge

gen daywk = dow(date)
recode daywk(0=0 "S")(1=1 "M")(2=2 "T")(3=3 "W")(4=4 "T")(5=5 "F")(6=6 "S"), into(dow)

preserve
gen stdate = date
keep if stdate<19512
collapse(sum) event, by(dow)
lab var event "#Events"

twoway bar event dow , ///
	tlabel(0 "S" 1 "M" 2 "T" 3 "W" 4 "T" 5 "F" 6 "S") ///
	aspectratio(1) ///
	xtitle("") ///
	ytitle(Protest events) legend(position(6) cols(1)) ///
	ylabel(0(100)400,nogrid) name(gg1, replace)
restore 

tsset date, daily
glm event i.dow, family(nbinom) vce(hac nwest 1)
coefplot, xline(0) drop(_cons) ///
	xlabel(-4 -2 0 2 4) ///
	aspectratio(1) ///
	name(gg2, replace)
	
gr combine gg1 gg2, name(g2, replace) title("Egypt")

**** Morocco ****

clear
import delimited 2011-01-01-2019-01-01-Northern_Africa-Morocco.csv, clear
gen stdate = date(event_date, "DMY")
gen event = 1
gen date = stdate
format date %tdn/d/Y
collapse(sum) event, by(date) 
save event_by_day.dta, replace

use expand, clear
merge 1:1 date using event_by_day.dta
sort date
replace event = 0 if event==.
drop _merge

gen daywk = dow(date)
recode daywk(0=0 "S")(1=1 "M")(2=2 "T")(3=3 "W")(4=4 "T")(5=5 "F")(6=6 "S"), into(dow)

preserve
collapse(sum) event, by(dow)
lab var event "#Events"

twoway bar event dow, ///
	tlabel(0 "S" 1 "M" 2 "T" 3 "W" 4 "T" 5 "F" 6 "S") ///
	aspectratio(1) ///
	xtitle("") ///
	ytitle(Protest events) legend(position(6) cols(1)) ///
	ylabel(0(100)300,nogrid) name(gg1, replace)
restore 

tsset date, daily
glm event ib1.dow, family(nbinom) vce(hac nwest 1)
coefplot, xline(0) drop(_cons) ///
	xlabel(-4 -2 0 2 4) ///
	aspectratio(1) ///
	name(gg2, replace)

gr combine gg1 gg2, name(g3, replace) title("Morocco")

**** Syria ****

clear
import delimited 2011-01-01-2019-01-01-Middle_East-Syria.csv, clear
gen stdate = date(event_date, "DMY")
gen event = 1
gen date = stdate
format date %tdn/d/Y
collapse(sum) event, by(date) 
save event_by_day.dta, replace

use expand, clear
merge 1:1 date using event_by_day.dta
sort date
replace event = 0 if event==.
drop _merge

gen daywk = dow(date)
recode daywk(0=0 "S")(1=1 "M")(2=2 "T")(3=3 "W")(4=4 "T")(5=5 "F")(6=6 "S"), into(dow)

preserve
collapse(sum) event, by(dow)
lab var event "#Events"

twoway bar event dow , ///
	tlabel(0 "S" 1 "M" 2 "T" 3 "W" 4 "T" 5 "F" 6 "S") ///
	aspectratio(1) ///
	xtitle("") ///
	ytitle(Protest events) legend(position(6) cols(1)) ///
	ylabel(0(100)500,nogrid) name(gg1, replace)
restore 

tsset date, daily
glm event i.dow, family(nbinom) vce(hac nwest 1)
coefplot, xline(0) drop(_cons) ///
	xlabel(-4 -2 0 2 4) ///
	aspectratio(1) ///
	name(gg2, replace)

gr combine gg1 gg2, name(g4, replace) title("Syria")

**** Bahrain ****

clear
import delimited 2011-01-01-2019-01-01-Middle_East-Bahrain.csv, clear
gen stdate = date(event_date, "DMY")
gen event = 1
gen date = stdate
format date %tdn/d/Y
collapse(sum) event, by(date) 
save event_by_day.dta, replace

use expand, clear
merge 1:1 date using event_by_day.dta
sort date
replace event = 0 if event==.
drop _merge

gen daywk = dow(date)
recode daywk(0=0 "S")(1=1 "M")(2=2 "T")(3=3 "W")(4=4 "T")(5=5 "F")(6=6 "S"), into(dow)

preserve
collapse(sum) event, by(dow)
lab var event "#Events"

twoway bar event dow , ///
	tlabel(0 "S" 1 "M" 2 "T" 3 "W" 4 "T" 5 "F" 6 "S") ///
	aspectratio(1) ///
	xtitle("") ///
	ytitle(Protest events) legend(position(6) cols(1)) ///
	ylabel(0(100)400,nogrid) name(gg1, replace)
restore 

tsset date, daily
glm event i.dow, family(nbinom) vce(hac nwest 1)
coefplot, xline(0) drop(_cons) ///
	xlabel(-4 -2 0 2 4) ///
	aspectratio(1) ///
	name(gg2, replace)
	
gr combine gg1 gg2, name(g5, replace) title("Bahrain")	

**** Jordan ****

clear
import delimited 2011-01-01-2019-01-01-Middle_East-Jordan.csv, clear
gen stdate = date(event_date, "DMY")
gen event = 1
gen date = stdate
format date %tdn/d/Y
collapse(sum) event, by(date) 
save event_by_day.dta, replace

use expand, clear
merge 1:1 date using event_by_day.dta
sort date
replace event = 0 if event==.
drop _merge

gen daywk = dow(date)
recode daywk(0=0 "S")(1=1 "M")(2=2 "T")(3=3 "W")(4=4 "T")(5=5 "F")(6=6 "S"), into(dow)

preserve
collapse(sum) event, by(dow)
lab var event "#Events"

twoway bar event dow , ///
	tlabel(0 "S" 1 "M" 2 "T" 3 "W" 4 "T" 5 "F" 6 "S") ///
	aspectratio(1) ///
	xtitle("") ///
	ytitle(Protest events) legend(position(6) cols(1)) ///
	ylabel(0(10)50,nogrid) name(gg1, replace)
restore 

tsset date, daily
glm event i.dow, family(nbinom) vce(hac nwest 1)
coefplot, xline(0) drop(_cons) ///
	xlabel(-4 -2 0 2 4) ///
	aspectratio(1) ///
	name(gg2, replace)
	
gr combine gg1 gg2, name(g6, replace) title("Jordan")	

**** Libya ****

clear
import delimited 2011-01-01-2019-01-01-Northern_Africa-Libya.csv, clear
gen stdate = date(event_date, "DMY")
gen event = 1
gen date = stdate
format date %tdn/d/Y
collapse(sum) event, by(date) 
save event_by_day.dta, replace

use expand, clear
merge 1:1 date using event_by_day.dta
sort date
replace event = 0 if event==.
drop _merge

gen daywk = dow(date)
recode daywk(0=0 "S")(1=1 "M")(2=2 "T")(3=3 "W")(4=4 "T")(5=5 "F")(6=6 "S"), into(dow)

preserve
collapse(sum) event, by(dow)
lab var event "#Events"

twoway bar event dow , ///
	tlabel(0 "S" 1 "M" 2 "T" 3 "W" 4 "T" 5 "F" 6 "S") ///
	aspectratio(1) ///
	xtitle("") ///
	ytitle(Protest events) legend(position(6) cols(1)) ///
	ylabel(0(100)400,nogrid) name(gg1, replace)
restore 

tsset date, daily
glm event i.dow, family(nbinom) vce(hac nwest 1)
coefplot, xline(0) drop(_cons) ///
	xlabel(-4 -2 0 2 4) ///
	aspectratio(1) ///
	name(gg2, replace)
	
gr combine gg1 gg2, name(g7, replace) title("Libya")

**** Combined graph ****

gr combine g1 g2 g3 g4 g5 g6 g7
	
